package io.github.harmonly.controller.api;

import io.github.harmonly.entity.association.*;
import io.github.harmonly.service.AssociationService;
import io.github.harmonly.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class AssociationController {

    @Resource
    private AssociationService service;

    /**
     * Association
     */
    @ResponseBody
    @RequestMapping(value = "/associations", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getAssociations() {
        return JSONUtil.toJSONString(service.getAssociations());
    }

    @ResponseBody
    @RequestMapping(value = "/association/{aid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getAssociation(@PathVariable("aid") int aid) {
        return JSONUtil.toJSONString(service.getAssociation(aid));
    }

    @ResponseBody
    @RequestMapping(value = "/association", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addAssociation(@RequestBody String json) {
        return JSONUtil.result(service.addAssociation(JSONUtil.parseObject(json, Association.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/association/{aid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteAssociation(@PathVariable("aid") int aid) {
        return JSONUtil.result(service.deleteAssociation(aid));
    }

    @ResponseBody
    @RequestMapping(value = "/association", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editAssociation(@RequestBody String json) {
        return JSONUtil.result(service.editAssociation(JSONUtil.parseObject(json, Association.class)));
    }

    /**
     * Association Honor
     */
    @ResponseBody
    @RequestMapping(value = "/honor", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getHonors() {
        return JSONUtil.toJSONString(service.getHonors());
    }

    @ResponseBody
    @RequestMapping(value = "/honor/{ahid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getHonor(@PathVariable("ahid") int ahid) {
        return JSONUtil.toJSONString(service.getHonor(ahid));
    }

    @ResponseBody
    @RequestMapping(value = "/honor", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addHonor(@RequestBody String json) {
        return JSONUtil.result(service.addHonor(JSONUtil.parseObject(json, AssociationHonor.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/honor/{ahid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteHonor(@PathVariable("ahid") int ahid) {
        return JSONUtil.result(service.deleteHonor(ahid));
    }

    @ResponseBody
    @RequestMapping(value = "/honor", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editHonor(@RequestBody String json) {
        return JSONUtil.result(service.editHonor(JSONUtil.parseObject(json, AssociationHonor.class)));
    }
}
